package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.Utils.DateUtils;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.PhysicalUploadDao;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.PhysicalUploadService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class PhysicalUploadServiceImpl implements PhysicalUploadService {

    @Autowired
    private PhysicalUploadDao physicalUploadDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetMealDao setMealDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     * @throws Exception
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) throws Exception {
        String queryString = queryPageBean.getQueryString();
        List<Member> members = memberDao.selectByCondition(queryString);
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<HashMap<String, Object>> rows = new ArrayList<>();
        for (Member member : members) {
            Integer memberId = member.getId();
            List<Order> orders = orderDao.findByMemberId(memberId);
            for (Order order : orders) {
                String orderStatus = order.getOrderStatus();
                //判断已经确定预约的订单
                if ("已到诊".equals(orderStatus)) {
                    Integer setmealId = order.getSetmealId();
                    SetMeal setMeal = setMealDao.findById(setmealId);
                    Integer orderId = order.getId();
                    PhysicalUpload physicalUpload1 = physicalUploadDao.findByOrderId(orderId);
                    //判断该订单是否已经存在数据库
                    if (physicalUpload1 == null) {
                        //该订单不存在数据库
                        physicalUploadDao.addByOrderId(orderId);
                        PhysicalUpload physicalUpload = physicalUploadDao.findByOrderId(orderId);
                        HashMap<String, Object> map = new HashMap<>();
                        Date Date = order.getOrderDate();
                        String orderDate = DateUtils.parseDate2String(Date, "yyyy-MM-dd");
                        map.put("id", physicalUpload.getId());
                        map.put("orderDate", orderDate);
                        map.put("fileNumber", member.getFileNumber());
                        map.put("name", member.getName());
                        map.put("phoneNumber", member.getPhoneNumber());
                        map.put("setmealName", setMeal.getName());
                        map.put("reportStatus", physicalUpload.getReportStatus());
                        rows.add(map);
                    } else {
                        HashMap<String, Object> map = new HashMap<>();
                        Date Date = order.getOrderDate();
                        String orderDate = DateUtils.parseDate2String(Date, "yyyy-MM-dd");
                        map.put("id", physicalUpload1.getId());
                        map.put("orderDate", orderDate);
                        map.put("fileNumber", member.getFileNumber());
                        map.put("name", member.getName());
                        map.put("phoneNumber", member.getPhoneNumber());
                        map.put("setmealName", setMeal.getName());
                        map.put("reportStatus", physicalUpload1.getReportStatus());
                        rows.add(map);
                    }
                }
            }
        }
        return new PageResult((long) rows.size(), rows);
    }

    /**
     * 体检上传添加
     * @param map //数据集合
     * @param setmealIds 套餐Id
     */
    @Override
    public void add(Map<String, String> map, Integer[] setmealIds) throws Exception {
        String memberName = map.get("name");
        String fileNumber = map.get("fileNumber");
        String phoneNumber = map.get("phoneNumber");
        String orderDate = map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        Member byTelephone = memberDao.findByTelephone(phoneNumber);
        //判断该手机号是否已经注册
        if (byTelephone==null) {
            Member member = new Member();
            member.setName(memberName);
            member.setPhoneNumber(phoneNumber);
            member.setFileNumber(fileNumber);
            memberDao.add(member);
        }else {
            byTelephone.setName(memberName);
            byTelephone.setFileNumber(fileNumber);
            memberDao.update(byTelephone);
        }
        Member member = memberDao.findByTelephone(phoneNumber);
        Integer memberId = member.getId();
        //判断是否选择套餐
        if (setmealIds.length==0) {
            Order order = new Order(memberId, date, null);
            order.setOrderType(Order.ORDERTYPE_TELEPHONE);
            order.setOrderStatus("已到诊");
            orderDao.add(order);
        }else {
            Order order = new Order(memberId, date, setmealIds[0]);
            order.setOrderType(Order.ORDERTYPE_TELEPHONE);
            order.setOrderStatus("已到诊");
            orderDao.add(order);
        }
    }

    @Override
    public PhysicalUpload findById(Integer id) {
        return physicalUploadDao.findById(id);
    }

    @Override
    public void uploadReport(PhysicalUpload physicalUpload) {
        physicalUpload.setReportStatus(2);
        physicalUploadDao.update(physicalUpload);
    }

    /**
     * 删除PhysicalUpload  并且会删除对应的order
     * @param id
     */
    @Override
    public void deletebyid(Integer id) {
        PhysicalUpload byId = physicalUploadDao.findById(id);
        Integer orderId = byId.getOrderId();
        orderDao.delete(orderId);
        physicalUploadDao.delete(id);
    }
}
